#include "entity.h"
#include <algorithm>


Entity::Entity()
	:mComponents()
{
}

Entity::~Entity()
{
	for(unsigned i=0; i < mComponents.size(); i++)
	{
		delete mComponents[i];
	}
}

void Entity::addComponent(EntityComponent *c)
{


	if (c == NULL)
		return;

	std::vector<EntityComponent*>::iterator it;
	it = std::find_if(mComponents.begin(), mComponents.end(), EqPred(c));
	if ( it != mComponents.end())
	{
		delete (*it);
		(*it) = c;
	}else
	{
		mComponents.push_back(c);
	}

}

EntityComponent *Entity::getComponent(ComponentType type)
{
	std::vector<EntityComponent*>::iterator it;
	it = std::find_if(mComponents.begin(), mComponents.end(), TypeEqPred(type));
	if (it != mComponents.end())
	{
		return *it;
	}else
	{
		return NULL;
	}
}

const EntityComponent *Entity::getComponent(ComponentType type)
	const
{
	std::vector<EntityComponent*>::const_iterator it;
	it = std::find_if(mComponents.begin(), mComponents.end(), TypeEqPred(type));
	if (it != mComponents.end())
	{
		return *it;
	}else
	{
		return NULL;
	}
}

void Entity::removeComponent(ComponentType type)
{
	std::vector<EntityComponent*>::iterator it;
	it = std::find_if(mComponents.begin(), mComponents.end(), TypeEqPred(type));
	if (it != mComponents.end())
	{
		mComponents.erase(it);
	}
}